<?php

namespace app\data\logic;

use app\data\service\CaseCategoryService;
use app\data\service\CaseService;
use app\data\service\CmsService;
use app\data\service\FeedBackService;
use app\data\service\JoinService;
use app\data\service\ProAttrJoinService;
use app\data\service\ProAttrService;
use app\data\service\ProCategoryService;
use app\data\service\ProductsExtCatRelationsService;
use app\data\service\ProImagesService;
use app\data\service\ProRelationCmsService;
use app\data\service\ProRelationDownloadService;
use app\data\service\ProRelationService;
use app\data\service\ProService;
use think\db\Query;
use think\facade\Log;

class ProductLogic extends BaseLogic
{
    protected ProCategoryService $proCategoryService;
    protected ProAttrService $proAttrService;
    protected ProAttrJoinService $proAttrJoinService;
    protected FeedBackService $feedbackService;
    protected CaseService $caseService;
    protected CaseCategoryService $caseCategoryService;
    protected JoinService $joinService;
    protected CmsService $cmsService;

    protected ProductsExtCatRelationsService $productsExtCatRelationsService;

    public function __construct()
    {
        $this->proCategoryService = new ProCategoryService();
        $this->proAttrService = new ProAttrService();
        $this->proAttrJoinService = new ProAttrJoinService();
        $this->feedbackService = new FeedBackService();
        $this->caseService = new CaseService();
        $this->caseCategoryService = new CaseCategoryService();
        $this->joinService = new JoinService();
        $this->cmsService = new CmsService();
        $this->productsExtCatRelationsService = new ProductsExtCatRelationsService();
    }

    public function add($obj, $data): array
    {
        $ret = match ($obj) {
            'pro_category' => $this->proCategoryService->insert($data),
            'pro_attr' => $this->proAttrService->insert($data),
            'feed_back' => $this->feedbackService->insert($data),
            'case' => $this->caseService->insertInfo($data),
            'caseCategory' => $this->caseCategoryService->insert($data)
        };
        if ($ret) {
            if ($obj == 'case') {
                $relation_product_ids = $data['relation_product_ids'];
                $relation_cms_ids = $data['relation_cms_ids'];
                if (isset($relation_product_ids)) {
                    $productIdsArr = explode(",", $relation_product_ids);
                    if (is_array($productIdsArr)) {
                        foreach ($productIdsArr as $item => $value) {
                            $info[] = [
                                'case_id' => $ret,
                                'product_id' => $value,
                                'create_time' => time(),
                                'update_time' => time(),
                                'delete_time' => 0,
                                'ver' => 0,
                            ];
                        }
                        $counts = $this->joinService->addCaseProductJoinData($info);
//                        if($counts > 0){
//                            return $this->responseInfo(true, 'SUCCESS', ['id' => $ret]);
//                        }
                    }
                }
                if (isset($relation_cms_ids)) {
                    $cmsIdsArr = explode(",", $relation_cms_ids);
                    if (is_array($cmsIdsArr)) {
                        foreach ($cmsIdsArr as $item => $value) {
                            $list[] = [
                                'case_id' => $ret,
                                'cms_id' => $value,
                                'create_time' => time(),
                                'update_time' => time(),
                                'delete_time' => 0,
                                'ver' => 0,
                            ];
                        }
                        $counts = $this->joinService->addCaseCmsJoinData($list);
//                        if($counts > 0){
//                            return $this->responseInfo(true, 'SUCCESS', ['id' => $ret]);
//                        }
                    }
                }
                return $this->responseInfo(true, 'SUCCESS', ['id' => $ret]);
            } else {
                return $this->responseInfo(true, 'SUCCESS', ['id' => $ret]);
            }
        } else {
            return $this->responseInfo(false, 'FAILURE');
        }
    }

    public function edit($obj, $data, $where): array
    {
        $ret = match ($obj) {
            'pro' => $this->proService->update($where, $data),
            'pro_category' => $this->proCategoryService->update($where, $data),
            'pro_attr' => $this->proAttrService->update($where, $data),
            'pro_images' => $this->proImagesService->update($where, $data),
            'pro_relation' => $this->proRelationService->update($where, $data),
            'pro_cms' => $this->proCmsService->update($where, $data),
            'pro_download' => $this->proDownloadService->update($where, $data),
            'case' => $this->caseService->update($where, $data),
            'caseCategory' => $this->caseCategoryService->update($where, $data)
        };
        if ($ret) {
            if ($obj == 'case') {
                $relation_product_ids = $data['relation_product_ids'];
                $relation_cms_ids = $data['relation_cms_ids'];
                if (isset($relation_product_ids)) {
                    $productIdsArr = explode(",", $relation_product_ids);
                    if (is_array($productIdsArr)) {
                        $del = $this->joinService->delCaseProductJoin(['case_id' => $where['id']]);
                        foreach ($productIdsArr as $item => $value) {
                            $info[] = [
                                'case_id' => $where['id'],
                                'product_id' => $value,
                                'create_time' => time(),
                                'update_time' => time(),
                                'delete_time' => 0,
                                'ver' => 0,
                            ];
                        }
                        $counts = $this->joinService->addCaseProductJoinData($info);
//                        if($counts > 0){
//                            return $this->responseInfo(true, 'SUCCESS', ['id' => $ret]);
//                        }
                    }
                }
                if (isset($relation_cms_ids)) {
                    $cmsIdsArr = explode(",", $relation_cms_ids);
                    if (is_array($cmsIdsArr)) {
                        $del = $this->joinService->delCaseCmsJoin(['case_id' => $where['id']]);
                        foreach ($cmsIdsArr as $item => $value) {
                            $list[] = [
                                'case_id' => $where['id'],
                                'cms_id' => $value,
                                'create_time' => time(),
                                'update_time' => time(),
                                'delete_time' => 0,
                                'ver' => 0,
                            ];
                        }
                        $counts = $this->joinService->addCaseCmsJoinData($list);
//                        if($counts > 0){
//                            return $this->responseInfo(true, 'SUCCESS', ['id' => $ret]);
//                        }
                    }
                }
                return $this->responseInfo(true, 'SUCCESS', ['id' => $ret]);
            } else {
                return $this->responseInfo(true, 'SUCCESS', ['id' => $ret]);
            }
        } else {
            return $this->responseInfo(false, 'FAILURE');
        }
    }

    public function getOne($obj, $field, $where): array
    {
        $result = match ($obj) {
            'pro' => $this->proService->getOne($field, $where, ['productExtCat' => function (Query $query) {
                $query->with(['parentCat' => function (Query $query) {
                    $query->where('parent_id', '<>', 0);
                }])->where('parent_id', '<>', 0);
            }]),
            'pro_category' => $this->proCategoryService->getOne($field, $where),
            'pro_attr' => $this->proAttrService->getOne($field, $where),
            'pro_images' => $this->proImagesService->getOne($field, $where),
            'pro_relation' => $this->proRelationService->getOne($field, $where),
            'pro_cms' => $this->proCmsService->getOne($field, $where),
            'pro_download' => $this->proDownloadService->getOne($field, $where),
            'pro_attr_join' => $this->proAttrJoinService->getOne($field, $where),
            'feed_back' => $this->feedbackService->getOne($field, $where),
            'case' => $this->caseService->getOne($field, $where, ['caseCategory']),
            'caseCategory' => $this->caseCategoryService->getOne($field, $where, ['cases'])
        };
        return $this->responseInfo(true, 'SUCCESS', $result);
    }

    public function getCount($obj, $where): array
    {
        $result = match ($obj) {
            'pro' => $this->proService->total($where),
            'pro_category' => $this->proCategoryService->total($where),
            'pro_attr' => $this->proAttrService->total($where),
            'pro_images' => $this->proImagesService->total($where),
            'pro_relation' => $this->proRelationService->total($where),
            'pro_cms' => $this->proCmsService->total($where),
            'pro_download' => $this->proDownloadService->total($where),
            'feed_back' => $this->feedbackService->total($where),
            'case' => $this->caseService->total($where),
            'caseCategory' => $this->caseCategoryService->total($where)
        };
        return $this->responseInfo(true, 'SUCCESS', $result);
    }

    public function getAll($obj, $field, $where, $order): array
    {
        $result = match ($obj) {
            'pro' => $this->proService->getAll($field, $where, $order, ['productOneCategory', 'productTwoCategory', 'productThreeCategory']),
            'pro_category' => $this->proCategoryService->getAll($field, $where, $order),
            'pro_attr' => $this->proAttrService->getAll($field, $where, $order),
            'pro_images' => $this->proImagesService->getAll($field, $where, $order),
            'pro_relation' => $this->proRelationService->getAll($field, $where, $order),
            'pro_cms' => $this->proCmsService->getAll($field, $where, $order),
            'pro_download' => $this->proDownloadService->getAll($field, $where, $order),
            'pro_attr_join' => $this->proAttrJoinService->getAll($field, $where, $order),
            'feed_back' => $this->feedbackService->getAll($field, $where, $order),
            'case' => $this->caseService->getAll($field, $where, $order, ['caseCategory']),
            'caseCategory' => $this->caseCategoryService->getAll($field, $where, $order, ['cases']),
            'join_case_product' => $this->joinService->getProductIdsByCaseId($where['id']),
            'join_case_cms' => $this->joinService->getCmsIdsByCaseId($where['id']),
            'cms' => $this->cmsService->getAll($field, $where, $order)
        };
        return $this->responseInfo(true, 'SUCCESS', $result);
    }

    public function getPage($obj, $page_index, $page_size, $where, $field, $order): array
    {
        $result = match ($obj) {
            'pro' => $this->proService->getPageList($page_index, $page_size, $where, $field, $order),
            'pro_category' => $this->proCategoryService->getPageList($page_index, $page_size, $where, $field, $order),
            'pro_attr' => $this->proAttrService->getPageList($page_index, $page_size, $where, $field, $order),
            'pro_images' => $this->proImagesService->getPageList($page_index, $page_size, $where, $field, $order),
            'pro_relation' => $this->proRelationService->getPageList($page_index, $page_size, $where, $field, $order),
            'pro_cms' => $this->proCmsService->getPageList($page_index, $page_size, $where, $field, $order),
            'pro_download' => $this->proDownloadService->getPageList($page_index, $page_size, $where, $field, $order),
            'feed_back' => $this->feedbackService->getPageList($page_index, $page_size, $where, $field, $order),
            'case' => $this->caseService->getPageList($page_index, $page_size, $where, $field, $order),
            'caseCategory' => $this->caseCategoryService->getPageList($page_index, $page_size, $where, $field, $order)
        };
        return $this->responseInfo(true, 'SUCCESS', $result);
    }

    public function del($obj, $where): array
    {
        $ret = match ($obj) {
            'pro' => $this->proService->softDel($where),
            'pro_category' => $this->proCategoryService->softDel($where),
            'pro_attr' => $this->proAttrService->softDel($where),
            'pro_images' => $this->proImagesService->softDel($where),
            'pro_relation' => $this->proRelationService->softDel($where),
            'pro_cms' => $this->proCmsService->softDel($where),
            'pro_download' => $this->proDownloadService->softDel($where),
            'case' => $this->caseService->softDel($where),
            'caseCategory' => $this->caseCategoryService->softDel($where)
        };
        if ($ret) {
            return $this->responseInfo(true, 'SUCCESS', ['id' => $ret]);
        } else {
            return $this->responseInfo(false, 'FAILURE');
        }
    }


    /**
     * 分页查询拓展分类下的产品
     * @param $params
     * @return mixed
     */
    public function getProductsByRelationsListByCatId($params)
    {
        $where = [];
        if (array_key_exists('cat_id', $params)) {
            $where[] = ['cat_id', '=', $params['cat_id']];
        }
        $field = "*";
        $order = 'id desc';
        $pageIndex = $params['page_index'] ?? 1;
        $pageSize = $params['page_size'] ?? 10;
        $result = $this->productsExtCatRelationsService->getPageList($pageIndex, $pageSize, $where, $field, $order);
        return $result;
    }

    /**
     * 获取拓展详情
     * @param $params
     * @return array
     */
    public function getProExtCatDetail($params)
    {
        $where = [];
        if (array_key_exists('product_id', $params)) {
            $where['product_id'] = $params['product_id'];
        }
        if (array_key_exists('cat_id', $params)) {
            $where['cat_id'] = $params['cat_id'];
        }
        $result = $this->productsExtCatRelationsService->getOne('*',$where);
        return $result;
    }

}